from openpyxl import load_workbook
import smtplib

from email.mime.text import MIMEText
from email.header import Header
# 加载excel文件
wb = load_workbook("", data_only=True)
sheet = wb.active

# 登录
smtp_obj = smtplib.SMTP_SSL("smtp.qq.com", 465)
smtp_obj.login("2339924507@qq.com", "XXXXXXXX")

for row in sheet.iter_rows(min_row=2):
    row_text = ""
    for cell in row:
        print(cell.value, end=", ")
        row_text += f"{cell.value}"
    name = row[3]
    sta_email = row[2].value
    print()
    mail_body_context = f"""
        <h3>{name.value}, 你好:</h3>
        <p>
            你喜欢编程吗，你喜欢算法吗？你喜欢新技术吗？
            <a href="https://t7.baidu.com/it/u=1819248061,230866778&fm=193&f=GIF">这是一张图片</a>
            {row_text}
        </p>
    """
    msg = MIMEText(mail_body_context, "html", "utf-8")
    msg["From"] = Header("来自我的问候", "utf-8")  # 发送者
    msg["To"] = Header("有缘人", "utf-8")  # 接收者
    msg["Subject"] = Header("娜美的信", "utf-8")  # 主题
    smtp_obj.sendmail("2339924507@qq.com", [sta_email], msg.as_string())
    print(f"成功发送工资条邮件{sta_email}, {name.value}")
